<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="岗位编码" prop="positionCode">
        <el-input
          v-model="queryParams.positionCode"
          placeholder="请输入岗位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位名称" prop="positionName">
        <el-input
          v-model="queryParams.positionName"
          placeholder="请输入岗位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="招聘数量" prop="positionNumber">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.positionNumber"-->
      <!--          placeholder="请输入招聘数量"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="年龄" prop="ageLimit">
        <el-input
          v-model="queryParams.ageLimit"
          placeholder="请输入当前年龄"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="薪酬范围">
        <el-col :span="10">
          <el-input
            v-model="queryParams.minSalary"
            placeholder="最低薪酬"
            clearable
            @keyup.enter.native="handleQuery"
            prop="minSalary"
          >

          </el-input>
        </el-col>
        <el-col class="line" :span="2" style="text-align: center;">-</el-col>
        <el-col :span="10">
          <el-input
            v-model="queryParams.maxSalary"
            placeholder="最高薪酬"
            clearable
            @keyup.enter.native="handleQuery"
            prop="maxSalary"
          >
            <template slot="append">元/月</template>
          </el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="学历要求" prop="educationaRequirements">
        <el-select
          v-model="queryParams.educationaRequirements"
          placeholder="请选择学历要求"
          clearable
          style="width: 100%"
          @keyup.enter.native="handleQuery"
        >
          <el-option label="不限" value="不限"></el-option>
          <el-option label="初中" value="初中"></el-option>
          <el-option label="高中" value="高中"></el-option>
          <el-option label="大专" value="大专"></el-option>
          <el-option label="本科" value="本科"></el-option>
          <el-option label="硕士" value="硕士"></el-option>
          <el-option label="博士" value="博士"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工作地点" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入工作地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="鉴定专业" prop="major">
        <el-input
          v-model="queryParams.requiredMajor"
          placeholder="请输入职业鉴定专业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="批次编号" prop="batchNumber">
        <el-input
          v-model="queryParams.batchNumber"
          placeholder="请输入批次编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 招聘岗位信息导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                 :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
                 :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport"/>
            是否更新已经存在的招聘岗位数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加导入按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:available:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:available:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:available:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
                   v-hasPermi="['system:available:import']">导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['system:available:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="availableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="岗位编码" align="center" prop="positionCode"/>
      <el-table-column label="岗位名称" align="center" prop="positionName"/>
      <el-table-column label="招聘数量" align="center" prop="positionNumber"/>
      <el-table-column label="年龄限制" align="center" prop="ageLimit"/>
      <el-table-column label="性别限制" align="center" prop="requiredSex">
        <template slot-scope="scope">
          <span>{{ { '0': '男', '1': '女', '2': '不限' }[scope.row.requiredSex] || '不限' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="岗位性质" align="center" prop="positionNature"/>
      <el-table-column label="薪资范围" align="center" prop="salary"/>
      <el-table-column label="学历要求" align="center" prop="educationaRequirements"/>
      <el-table-column label="工作地点" align="center" prop="location"/>
      <el-table-column label="鉴定专业" align="center" prop="major"/>
      <el-table-column label="鉴定等级" align="center" prop="level"/>
      <el-table-column label="工作经历" align="center" prop="workExperienceRequirements"/>
      <el-table-column label="批次编号" align="center" prop="batchNumber"/>
      <el-table-column label="状   态" align="center" prop="status">
        <template slot-scope="scope"><span>{{ scope.row.status === '0' ? '正常' : '停用' }}</span></template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:available:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :page-sizes="[10, 20, 30, 50, 100,10000]"
      @pagination="getList"
    />


    <!-- 添加或修改招聘岗位信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="岗位编码" prop="positionCode">
          <el-input v-model="form.positionCode" placeholder="请输入岗位编码"/>
        </el-form-item>
        <el-form-item label="岗位名称" prop="positionName">
          <el-input v-model="form.positionName" placeholder="请输入岗位名称"/>
        </el-form-item>
        <el-form-item label="招聘数量" prop="positionNumber">
          <el-input v-model="form.positionNumber" placeholder="请输入招聘数量"/>
        </el-form-item>
        <el-form-item label="年龄限制" prop="ageLimit">
          <el-input v-model="form.ageLimit" placeholder="请输入年龄限制['xx以上'、'xx以下'、‘xx-xx之间’‘不限’]"/>
        </el-form-item>
        <el-form-item label="性别限制" prop="requiredSex">
          <el-input v-model="form.ageLimit" placeholder="请输入性别限制[‘男’、‘女’、‘不限’]"/>
        </el-form-item>
        <el-form-item label="显示顺序" prop="positionSort">
          <el-input v-model="form.positionSort" placeholder="请输入显示顺序"/>
        </el-form-item>
        <el-form-item label="岗位性质" prop="positionNature">
          <el-input v-model="form.positionNature" placeholder="请输入岗位性质"/>
        </el-form-item>
        <el-form-item label="薪资范围" prop="salary">
          <el-input v-model="form.salary" placeholder="请输入薪酬范围[‘xxx元/月’、‘xxx-xxx元/月’]"/>
        </el-form-item>
        <el-form-item label="学历要求" prop="educationaRequirements">
          <el-input v-model="form.educationaRequirements" placeholder="请输入学历要求"/>
        </el-form-item>
        <el-form-item label="工作地点" prop="location">
          <el-input v-model="form.location" placeholder="请输入工作地点"/>
        </el-form-item>
        <el-form-item label="鉴定专业" prop="major">
          <el-input v-model="form.requiredMajor" placeholder="请输入职业鉴定专业"/>
        </el-form-item>
        <el-form-item label="鉴定等级" prop="level">
          <el-input v-model="form.requiredLevel" placeholder="请输入职业技能鉴定等级"/>
        </el-form-item>
        <el-form-item label="工作经历" prop="workExperienceRequirements">
          <el-input v-model="form.workExperienceRequirements" placeholder="请输入工作经历要求"/>
        </el-form-item>
        <el-form-item label="批次编号" prop="batchNumber">
          <el-input v-model="form.batchNumber" placeholder="请输入批次编号"/>
        </el-form-item>
        <el-form-item label="备    注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listAvailable, getAvailable, delAvailable, addAvailable, updateAvailable} from "@/api/system/available";
  import {getToken} from "@/utils/auth";

  export default {
    name: "Available",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 招聘岗位信息表格数据
        availableList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          positionCode: null,
          positionName: null,
          positionNumber: null,
          ageLimit: null,
          positionSort: null,
          positionNature: null,
          salary: null,
          minSalary:null,
          maxSalary:null,
          educationaRequirements: null,
          location: null,
          requiredMajor: null,
          requiredLevel: null,
          workExperienceRequirements: null,
          batchNumber: null,
          status: null,
        },
        // 岗位表单参数
        form: {},


        // 导入参数
        upload: {
          open: false,
          title: "",
          isUploading: false,
          updateSupport: 0,
          headers: {Authorization: "Bearer " + getToken()},
          url: process.env.VUE_APP_BASE_API + "/system/available/importData"
        },
        // 表单校验
        rules: {
          positionCode: [
            {required: true, message: "岗位编码不能为空", trigger: "blur"}
          ],
          positionName: [
            {required: true, message: "岗位名称不能为空", trigger: "blur"}
          ],
          positionSort: [
            {required: true, message: "显示顺序不能为空", trigger: "blur"}
          ],
          batchNumber: [
            {required: true, message: "批次编号不能为空", trigger: "blur"}
          ],
          status: [
            {required: true, message: "状态不能为空", trigger: "change"}
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询招聘岗位信息列表 */
      getList() {
        this.loading = true;
        listAvailable(this.queryParams).then(response => {
          this.availableList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          positionId: null,
          positionCode: null,
          positionName: null,
          positionNumber: null,
          ageLimit: null,
          positionSort: null,
          positionNature: null,
          salary: null,
          educationaRequirements: null,
          location: null,
          major: null,
          level: null,
          workExperienceRequirements: null,
          batchNumber: null,
          status: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        // 处理年龄筛选 - 用户输入具体年龄，匹配数据库中的年龄要求
        // 保留原始ageLimit用于显示
        const originalAgeLimit = this.queryParams.ageLimit;
        // 例如：用户输入年龄25，需要匹配数据库中"25以上"、"20-30之间"等规则
        if (this.queryParams.ageLimit && !isNaN(this.queryParams.ageLimit)) {
          // 如果用户输入的是具体年龄数字，则保存为查询参数
          this.queryParams.userAge = parseInt(this.queryParams.ageLimit);

          // 清空ageLimit避免冲突
          this.queryParams.ageLimit = '';
        }

        this.getList();
        this.queryParams.ageLimit = originalAgeLimit;
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.positionId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加招聘岗位信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const positionCode = row.positionCode || this.ids
        getAvailable(positionCode).then(response => {
          this.form = response.data;
          console.log(this.form)
          this.open = true;
          this.title = "修改招聘岗位信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.positionId != null) {
              updateAvailable(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addAvailable(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const positionIds = row.positionId || this.ids;
        this.$modal.confirm('是否确认删除招聘岗位信息编号为"' + positionIds + '"的数据项？').then(function () {
          return delAvailable(positionIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },

      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "招聘岗位信息导入";
        this.upload.open = true;
      },
      /** 下载模板操作 */
      importTemplate() {
        this.download('system/available/importTemplate', {}, `available_template_${new Date().getTime()}.xlsx`);
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
        this.getList();
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      },

      /** 导出按钮操作 */
      handleExport() {
        this.download('system/available/export', {
          ...this.queryParams
        }, `available_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
